package com.example.daiyinuo.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ProxyFactoryStudent implements MethodInterceptor {
    //维护一个目标对象
    private Object target;
    public ProxyFactoryStudent(Object target){
        this.target = target;
    }
    public Object getProxyInstance(){
        //1.创建一个工具类
        Enhancer enhancer = new Enhancer();
        //2.设置父类
        enhancer.setSuperclass(target.getClass());
        //3.设置回调函数
        enhancer.setCallback(this);
        //4.创建子类对象，即代理对象
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("Cglib代理模式  开始");
        Object returnVal = method.invoke(target, objects);
        System.out.println("Cglib代理模式 提交");
        return returnVal;
    }
}
